import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
var _routeMatch;
import { createModel, init } from "@rematch/core";
import { getUserInfo } from "../api/user";
import { getTokenAssets, getAnnouncementCount, getMessageCount } from "../api/wallet";
import { getCountry } from "../api/auth";
import { getCoinListAll } from "../api/contract";
import { matchRoute } from "../utils/match";
import { EVENT, KEYCONST } from "../enums";
import toast from "../compoents/toast";
import kingsInstance from "../instance";
import { getMyInviter } from "../api/user";
import Big from "big.js";
import { isDevChain, isDomesticVersion, TEST_CHAIN, MAIN_CHAIN } from "../config";
import i18n from "../local";
Big.NE = -15;
export var routes = [
    {
        path: "/otherLogin",
        component: function() {
            return import(/* webpackChunkName:"otherLogin" */ "../wallet/sign-in");
        },
        meta: {
            navbar: false,
            fullScreen: false
        }
    },
    {
        path: "/register",
        component: function() {
            return import(/* webpackChunkName:"register" */ "../wallet/sign-up");
        },
        meta: {
            navbar: false,
            fullScreen: false
        }
    },
    {
        path: "/invitecode",
        name: "Invitecode",
        // component: Login,
        component: function() {
            return import(/* webpackChunkName: "invitecode" */ "../wallet/invitecode");
        },
        meta: {
            // keepAlive: true
            navbar: false
        }
    },
    {
        path: "/forgetpass",
        name: "Forgetpass",
        // component: Login,
        component: function() {
            return import(/* webpackChunkName: "forgetpass" */ "../wallet/forget-password");
        },
        meta: {
            // keepAlive: true
            navbar: false,
            fullScreen: false
        }
    },
    {
        path: "/home",
        name: "Home",
        component: function() {
            return import(/* webpackChunkName: "home" */ "../wallet/home/home");
        },
        meta: {
            // keepAlive: true
            navbar: true,
            id: "home"
        }
    },
    // {
    //   path: '/download',
    //   name: 'Download',
    //   // component: Login,
    //   component: () =>
    //     import(/* webpackChunkName: "download" */ '@/wallet/download'),
    //   meta: {
    //     // keepAlive: true
    //     navbar: true,
    //     id: 'download',
    //   },
    // },
    {
        path: "/nftDetails",
        name: "nftDetails",
        // component: Login,
        component: function() {
            return import(/* webpackChunkName: "nftDetails" */ "../wallet/nft-detail");
        },
        meta: {
            /** 允许未登录状态访问 */ allow: true
        }
    },
    // {
    //   path: '/nftsend',
    //   name: 'Nftsend',
    //   // component: Login,
    //   component: () =>
    //     import(/* webpackChunkName: "nftsend" */ '@/wallet/nftend'),
    //   meta: {
    //     // keepAlive: true
    //     navbar: false,
    //   },
    // },
    {
        path: "/sendToken",
        name: "sendToken",
        component: function() {
            return import(/* webpackChunkName:"sendToken" */ "../wallet/send-token");
        }
    },
    {
        path: "/receive",
        name: "receive",
        component: function() {
            return import(/* webpackChunkName:"receive" */ "../wallet/receive");
        }
    },
    {
        path: "/txn",
        name: "txn",
        component: function() {
            return import(/* webpackChunkName:"txn" */ "../wallet/txn");
        }
    },
    {
        path: "/nftsHistory",
        name: "nftsHistory",
        component: function() {
            return import(/* webpackChunkName:"nftsHistory" */ "../wallet/nfts-history");
        }
    },
    {
        path: "/addToken",
        name: "addToken",
        component: function() {
            return import(/* webpackChunkName:"addToken" */ "../wallet/add-token");
        }
    },
    {
        path: "/changePassword",
        name: "changePassword",
        component: function() {
            return import(/* webpackChunkName:"changePassword" */ "../wallet/change-password");
        }
    },
    {
        path: "/login",
        component: function() {
            return import(/* webpackChunkName:"login" */ "../wallet/account-login");
        },
        meta: {
            navbar: false,
            fullScreen: false
        }
    },
    // {
    //   path:'/invitationCode',
    //   name:'/invitationCode',
    //   component:()=>import(/*  webpackChunkName:"invitationCode" */ '@/wallet/invitation-code')
    // },
    {
        path: "/personalInformation",
        name: "personalInformation",
        component: function() {
            return import(/* webpackChunkName:'personalInformation' */ "../wallet/personal-information");
        }
    },
    {
        path: "/editProfile",
        name: "editProfile",
        component: function() {
            return import(/* webpackChunkName: 'editProfile' */ "../wallet/edit-profile");
        }
    },
    {
        path: "/setUsername",
        name: "setUsername",
        component: function() {
            return import(/* webpackChunkName: 'setUsername' */ "../wallet/set-username");
        }
    },
    {
        path: "/referralCode",
        name: "referralCOde",
        component: function() {
            return import(/* webpackChunkName: 'referralCode' */ "../wallet/referral");
        }
    },
    {
        path: "/accountBind",
        name: "accountBind",
        component: function() {
            return import(/* webpackChunkName: 'accountBind' */ "../wallet/account-bind");
        }
    },
    {
        path: "/notifications",
        name: "notifications",
        component: function() {
            return import(/* webpackChunkName: 'notifications' */ "../wallet/notifications");
        }
    },
    {
        path: "/myReferral",
        name: "myReferral",
        component: function() {
            return import(/* webpackChunkName: 'myReferral' */ "../wallet/my-referral");
        }
    },
    {
        path: "/share",
        name: "share",
        component: function() {
            return import(/* webpackChunkName: 'share' */ "../wallet/share");
        }
    },
    {
        path: "/scanRecord",
        name: "scanRecord",
        component: function() {
            return import(/* webpackChunkName: 'scanRecord' */ "../wallet/event-record");
        }
    }
];
export var routeMatch = matchRoute(routes);
var app = createModel()({
    name: "app",
    state: {
        userInfo: {
            avatarLink: "",
            nickname: "",
            email: "",
            phone: "",
            id: "",
            did: "",
            bnbAddress: "",
            ethAddress: "",
            cfxAddress: "",
            bnbTestAddress: "",
            ethTestAddress: "",
            cfxTestAddress: ""
        },
        //用户信息
        notice: {
            totalAnnouncementCount: 0,
            readAnnouncementCount: 0,
            totalMessageCount: 0,
            readMessageCount: 0
        },
        inviter: {},
        chain: isDomesticVersion ? "CONFLUX" : JSON.parse(localStorage.getItem(KEYCONST.localStore) || "{}").chain || "ETHEREUM",
        chainNft: isDomesticVersion ? "CONFLUX" : JSON.parse(localStorage.getItem(KEYCONST.localStore) || "{}").chainNft || "ETHEREUM",
        chainList: isDomesticVersion ? [
            "CONFLUX"
        ] : [
            "ETHEREUM",
            "BSC",
            "CONFLUX"
        ],
        tokenCoins: [],
        //币种信息
        nftCoins: [],
        //nft 的币种信息
        testnet: false,
        // path: 'login' as Route,
        path: "/home",
        prevPath: "/home",
        tokenAssets: {},
        meta: ((_routeMatch = routeMatch("/home")) === null || _routeMatch === void 0 ? void 0 : _routeMatch.meta) || {
            navbar: false
        },
        loading: false,
        walletVisible: false,
        payPasswordModal: {
            show: false,
            type: "INPUT"
        },
        oauth2: {},
        history: [
            "/home"
        ],
        firstRender: true,
        maskOpen: false,
        triggerEl: null,
        isLogin: false,
        accountInfo: {},
        //用户资产信息
        theme: localStorage.getItem(KEYCONST.theme) || "dark",
        /** 国际化 */ currentLocal: i18n.language,
        areaCodes: []
    },
    reducers: {
        SET_USER_INFO: function(state, userInfo) {
            var newUserInfo = _object_spread({}, state.userInfo, userInfo);
            return _object_spread_props(_object_spread({}, state), {
                userInfo: newUserInfo,
                isLogin: !!newUserInfo.id
            });
        },
        SET_NOTICE: function(state, notice) {
            var newNotice = _object_spread({}, state.notice, notice);
            return _object_spread_props(_object_spread({}, state), {
                notice: newNotice
            });
        },
        /** 设置邀请人 */ SET_INVITER: function(state, inviter) {
            return _object_spread_props(_object_spread({}, state), {
                inviter: inviter || {}
            });
        },
        //删除用户信息
        DELETE_USER_INFO: function(state) {
            // 设置用户信息
            var userInfo = {
                avatarLink: "",
                nickname: "",
                email: "",
                phone: "",
                id: "",
                did: "",
                inviter: "",
                bnbAddress: "",
                ethAddress: "",
                cfxAddress: "",
                bnbTestAddress: "",
                ethTestAddress: "",
                cfxTestAddress: ""
            };
            // uni.removeStorageSync('token')
            localStorage.setItem(KEYCONST.token, "");
            localStorage.setItem(KEYCONST.userInfo, "");
            return _object_spread_props(_object_spread({}, state), {
                userInfo: userInfo,
                isLogin: false
            });
        },
        //设置用户信息
        SET_TOKEN_COINS: function(state, coininfo) {
            // 设置用户信息
            // state.coininfo = coininfo;
            return _object_spread_props(_object_spread({}, state), {
                tokenCoins: coininfo
            });
        },
        // 设置nft类型信息
        SET_NFT_COINS: function(state, nftTypes) {
            return _object_spread_props(_object_spread({}, state), {
                nftCoins: nftTypes
            });
        },
        PUSH: function(state, newPath) {
            var _routeMatch;
            var pathname = typeof newPath === "string" ? newPath : newPath.path;
            // state.path = newPath;
            // state.meta = routeMatch(pathname)?.meta || {};
            return _object_spread_props(_object_spread({}, state), {
                prevPath: state.path,
                path: newPath,
                meta: ((_routeMatch = routeMatch(pathname)) === null || _routeMatch === void 0 ? void 0 : _routeMatch.meta) || {},
                history: state.history.concat([
                    newPath
                ])
            });
        },
        SET_META: function(state, payload) {
            // state.meta = payload;
            return _object_spread_props(_object_spread({}, state), {
                meta: payload
            });
        },
        SET_LOADING: function(state, payload) {
            return _object_spread_props(_object_spread({}, state), {
                loading: payload
            });
        },
        SWITCH_VISIBLE: function(state, payload, el) {
            return _object_spread_props(_object_spread({}, state), {
                triggerEl: el,
                walletVisible: payload
            });
        },
        SWITCH_PAY_VISIBLE: function(state, payload) {
            return _object_spread_props(_object_spread({}, state), {
                payPasswordModal: _object_spread_props(_object_spread({}, state.payPasswordModal), {
                    show: payload
                })
            });
        },
        SET_PAYPASSWORD_CONFIG: function(state, payload) {
            return _object_spread_props(_object_spread({}, state), {
                payPasswordModal: payload
            });
        },
        BACK: function(state, payload) {
            var _routeMatch;
            state.history.pop();
            var prevPath = state.history[state.history.length - 1];
            var pathname = typeof prevPath === "string" ? prevPath : prevPath.path;
            if (payload) {
                prevPath = {
                    path: pathname,
                    query: _object_spread({}, prevPath.query || {}, payload)
                };
            }
            return _object_spread_props(_object_spread({}, state), {
                path: prevPath || "/home",
                meta: ((_routeMatch = routeMatch(pathname)) === null || _routeMatch === void 0 ? void 0 : _routeMatch.meta) || {}
            });
        },
        RENDER: function(state) {
            return _object_spread_props(_object_spread({}, state), {
                firstRender: false
            });
        },
        SWITCH_MASK_STATUS: function(state, payload) {
            return _object_spread_props(_object_spread({}, state), {
                maskOpen: payload
            });
        },
        // 登录状态
        SET_LOGIN_STATUS: function(state, isLogin) {
            return _object_spread_props(_object_spread({}, state), {
                isLogin: isLogin
            });
        },
        // 用户资产
        SET_ACCOUNT_INFO: function(state, accountInfo) {
            return _object_spread_props(_object_spread({}, state), {
                accountInfo: accountInfo
            });
        },
        // theme
        SET_THEME: function(state, theme) {
            localStorage.setItem(KEYCONST.theme, theme);
            return _object_spread_props(_object_spread({}, state), {
                theme: theme
            });
        },
        //auth2
        SET_OAUTH2: function(state, payload) {
            return _object_spread_props(_object_spread({}, state), {
                oauth2: payload
            });
        },
        /** 修改链 true 测试链 false 正式链 */ SET_NETWORK: function(state, payload) {
            return _object_spread_props(_object_spread({}, state), {
                testnet: payload
            });
        },
        SET_CHAIN_NFT: function(state, payload) {
            return _object_spread_props(_object_spread({}, state), {
                chainNft: payload
            });
        },
        /** 修改token链 */ SET_CHAIN: function(state, payload) {
            return _object_spread_props(_object_spread({}, state), {
                chain: payload
            });
        },
        SET_CURRENT_LOCAL: function(state, payload) {
            return _object_spread_props(_object_spread({}, state), {
                currentLocal: payload
            });
        },
        /** 筛选支持链 */ SET_CHAIN_LIST: function(state, payload) {
            var newChainList = _to_consumable_array(new Set(payload));
            return _object_spread_props(_object_spread({}, state), {
                chainList: newChainList
            });
        },
        /** 修改区号区间 */ SET_AREA_CODE: function(state, payload) {
            return _object_spread_props(_object_spread({}, state), {
                areaCodes: payload
            });
        }
    },
    effects: function(dispatch) {
        return {
            initNotice: function initNotice() {
                return Promise.all([
                    getAnnouncementCount(),
                    getMessageCount()
                ]).then(function(param) {
                    var _param = _sliced_to_array(param, 2), announcementCountData = _param[0], messageCountData = _param[1];
                    if (announcementCountData.code == 200 || messageCountData.code == 200) {
                        var _messageCountData_data;
                        var messageCount = messageCountData === null || messageCountData === void 0 ? void 0 : (_messageCountData_data = messageCountData.data) === null || _messageCountData_data === void 0 ? void 0 : _messageCountData_data.total;
                        var announcementCount = announcementCountData === null || announcementCountData === void 0 ? void 0 : announcementCountData.data;
                        var noticeLocalStorageStr = localStorage.getItem(KEYCONST.notice) || "{}";
                        var noticeLocalStorageObj = JSON.parse(noticeLocalStorageStr);
                        var noticeObj = _object_spread_props(_object_spread({}, noticeLocalStorageObj), {
                            totalMessageCount: messageCount,
                            totalAnnouncementCount: announcementCount
                        });
                        dispatch.app.SET_NOTICE(noticeObj);
                        localStorage.setItem(KEYCONST.notice, JSON.stringify(_object_spread({}, noticeObj)));
                        kingsInstance.emit(EVENT.notice, noticeObj);
                    }
                });
            },
            setUserInfo: function setUserInfo() {
                // @ts-ignore
                return Promise.all([
                    getUserInfo(),
                    getMyInviter()
                ]).then(function(param) {
                    var _param = _sliced_to_array(param, 2), userData = _param[0], inviterData = _param[1];
                    if (userData.code == 200 || inviterData.code == 200) {
                        localStorage.setItem(KEYCONST.userInfo, JSON.stringify(userData.data));
                        dispatch.app.SET_USER_INFO(userData.data);
                        dispatch.app.SET_INVITER(inviterData.data);
                        return {
                            code: 200,
                            msg: "",
                            data: userData.data
                        };
                    } else {
                        throw userData.data.msg ? userData : inviterData;
                    }
                });
            },
            deleteUserInfo: function deleteUserInfo() {
                localStorage.removeItem(KEYCONST.token);
                localStorage.removeItem(KEYCONST.userInfo);
                dispatch.app.DELETE_USER_INFO();
            },
            setCoinInfos: function setCoinInfos() {
                return getCoinListAll(window.CHAIN_TYPE).then(function(res) {
                    // 过滤 测试链或者正式链
                    res.data = (res.data || []).filter(function(coin) {
                        if (window.CHAIN_TYPE) {
                            return [
                                window.CHAIN_TYPE
                            ].includes(coin.chainType);
                        }
                        if (!isDevChain) {
                            return !TEST_CHAIN.includes(coin.chainType);
                        }
                        return true;
                    });
                    /** token 类型 */ var tokenTypes = (res.data || []).filter(function(coin) {
                        return ![
                            "ERC721",
                            "ERC1155"
                        ].includes(coin === null || coin === void 0 ? void 0 : coin.spec);
                    });
                    /** nft 类型 */ var nftTypes = (res.data || []).filter(function(coin) {
                        return [
                            "ERC721",
                            "ERC1155"
                        ].includes(coin === null || coin === void 0 ? void 0 : coin.spec);
                    });
                    dispatch.app.SET_TOKEN_COINS(tokenTypes);
                    dispatch.app.SET_NFT_COINS(nftTypes);
                    localStorage.setItem(KEYCONST.tokenCoins, JSON.stringify(tokenTypes));
                    localStorage.setItem(KEYCONST.nftCoins, JSON.stringify(nftTypes));
                }).catch(function(err) {
                    console.log("err", err);
                });
            },
            //login in
            login: function login(el) {
                /** 判断是否登录 登录是否过期 如果没过期直接登录 */ return getUserInfo().then(function(res) {
                    if (res.code == 200) {
                        dispatch.app.SET_USER_INFO(res.data);
                        return res;
                    } else {
                        /** 过期期或者其他 */ dispatch.app.PUSH("/login");
                        dispatch.app.SWITCH_VISIBLE(true, el);
                    }
                });
            },
            logout: function logout() {
                var destroy = toast.confirm({
                    title: "Are you sure you want to sign out?",
                    onConfirm: function() {
                        destroy();
                        dispatch.app.deleteUserInfo();
                        kingsInstance.emit(EVENT.close);
                    }
                }).destroy;
            },
            setInviter: function setInviter() {
                return getMyInviter().then(function(res) {
                    if (res.code == 200) {
                        dispatch.app.SET_INVITER(res.data);
                    }
                });
            },
            /** 计算总资产 */ setAccountInfo: function setAccountInfo() {
                return Promise.all([
                    getTokenAssets({
                        pageNum: 1,
                        pageSize: 1000,
                        chainType: isDevChain ? TEST_CHAIN : MAIN_CHAIN
                    }),
                    getCoinListAll(window.CHAIN_TYPE)
                ]).then(function(param) {
                    var _param = _sliced_to_array(param, 2), res = _param[0], list = _param[1];
                    if (res.code == 200 && list.code == 200) {
                        var assets = res.data.list || [];
                        // 过滤 测试链或者正式链
                        list.data = (list.data || []).filter(function(coin) {
                            if (window.CHAIN_TYPE) {
                                return [
                                    window.CHAIN_TYPE
                                ].includes(coin.chainType);
                            }
                            if (!isDevChain) {
                                return !TEST_CHAIN.includes(coin.chainType);
                            }
                            return true;
                        });
                        // const coinMap = list.data.reduce((pre, next) => {
                        //   pre[
                        //     `${next.chainType}${next.contractAddress.toLocaleLowerCase()}`
                        //   ] = next;
                        //   return pre;
                        // }, {} as Record<string, CoinType>);
                        var accountAmount = (list.data || []).map(function(next) {
                            var inAsset = assets.find(function(a) {
                                return "".concat(next.chainType).concat(next.contractAddress.toLocaleLowerCase()) === "".concat(a.chainType).concat(a.contract.toLocaleLowerCase());
                            });
                            return _object_spread_props(_object_spread({
                                coinId: next.id
                            }, next), {
                                uniqueId: "".concat(next.chainType).concat(next.contractAddress.toLocaleLowerCase()),
                                coinName: next.name,
                                convertActualAmount: inAsset ? new Big(inAsset.balance || 0).mul(next.price2USD).div("1e".concat(next.digits)).toString() : "0",
                                balance: (inAsset === null || inAsset === void 0 ? void 0 : inAsset.balance) || "0",
                                formatBalance: new Big((inAsset === null || inAsset === void 0 ? void 0 : inAsset.balance) || "0").div("1e".concat(next.digits)).toString()
                            });
                        });
                        var accountInfo = {
                            totalActualAmount: accountAmount.reduce(function(pre, next) {
                                return new Big(pre).plus(next.convertActualAmount).toString();
                            }, "0"),
                            accountAmount: accountAmount
                        };
                        dispatch.app.SET_ACCOUNT_INFO(accountInfo);
                        return accountInfo;
                    } else {
                        throw res;
                    }
                }).catch(function(err) {
                    console.log(err, "error");
                });
            },
            /** 获取区号 */ getAreaCodes: function getAreaCodes() {
                return getCountry(kingsInstance.authConfig.ONA_APPID).then(function(res) {
                    if (res.code == 200) {
                        var data = JSON.parse(res.data);
                        dispatch.app.SET_AREA_CODE(data);
                    }
                    return res;
                });
            }
        };
    }
});
var models = {
    app: app
};
export var store = init({
    models: models
});

 //# sourceMappingURL=index.js.map